#include "ParamRepOp.h"
#include "ParamRepDialog.h"
#include "../../detiq-t/GenericInterface/Widgets/ImageWidgets/ImageWindow.h"
#include "../../detiq-t/GenericInterface/Widgets/ImageWidgets/StandardImageWindow.h"
#include "../../detiq-t/GenericInterface/Widgets/ImageWidgets/DoubleImageWindow.h"
#include <QApplication>

ParamRepOp::ParamRepOp(): GenericOperation(qApp->translate("Operations", "Change &Parameter of image").toStdString()) {
    initShortcut(new QKeySequence(QString("Ctrl+P")));
    fromWindow = false;
}

void ParamRepOp::operator()(const genericinterface::ImageWindow* currentWnd, const std::vector<const genericinterface::ImageWindow*>&){
    genericinterface::ImageWindow::Mode mode= genericinterface::ImageWindow::USER;
    if(fromWindow)
        mode= genericinterface::ImageWindow::CONS;
    ParamRepDialog* dialog = new ParamRepDialog(NULL,currentWnd,mode);

    QDialog::DialogCode code = static_cast<QDialog::DialogCode>(dialog->exec());

    if(code!=QDialog::Accepted) {
        return;
    }

    ParamRep * param = dialog->getParam();


    /**
    if(param->getParam(ParamRep::IMMEMORY)){
        //When modify image in memory, need to define the output of the operation according to the parameter choosen
        if(currentWnd->isStandard()){
            StandardImageWindow * siw = const_cast<StandardImageWindow *>(dynamic_cast<const StandardImageWindow*>(currentWnd));
            if(param->getParam(ParamRep::CONVERT)){//we create a new window with process image and convert
                if(param->getParam(ParamRep::CONBEFORE)){//we convert before processing
                    Image_t<double> * dblimage = Converter<Image_t<double> >::convert(*(siw->getImage()));//we simply convert
                    this->outDoubleImage(Converter<Image_t<double> >::applyProcessing(dblimage,param),"convert");
                }else{
                    this->outDoubleImage(Converter<Image_t<double> >::applyProcessing(siw->getImage(),param),"convert");
                }
            }else{
                if(param->getParam(ParamRep::NEWIM)){
                    this->outImage(Converter<Image_t<int> >::applyProcessing(siw->getImage(),param),"new");
                }else{
                    siw->setMemoryImage(Converter<Image_t<int> >::applyProcessing(siw->getImage(),param));
                }
            }
        }else if(currentWnd->isDouble()){
            DoubleImageWindow * diw = const_cast<DoubleImageWindow *>(dynamic_cast<const DoubleImageWindow*>(currentWnd));
            if(param->getParam(ParamRep::CONVERT)){//we create a new window with process image and convert
                if(param->getParam(ParamRep::CONBEFORE)){//we convert before processing
                    Image_t<int> * stdimage = Converter<Image_t<int> >::convertAndRound(*(diw->getImage()));//we simply convert
                    this->outImage(Converter<Image_t<int> >::applyProcessing(stdimage,param),"convert");
                }else{
                    Image_t<double> *dblimage = Converter<Image_t<double> >::applyProcessing(diw->getImage(),param);
                    this->outImage(Converter<Image_t<int> >::applyProcessing(dblimage,param),"new");
                }

            }else{
                if(param->getParam(ParamRep::NEWIM)){
                    this->outDoubleImage(Converter<Image_t<double> >::applyProcessing(diw->getImage(),param),"new");
                }else{
                    diw->setMemoryImage(Converter<Image_t<double> >::applyProcessing(diw->getImage(),param));
                }
            }
        }

    }else{
        //We modify the image display just applying setting to the window the it will display the correct window
        //We change the parameter of the windows then we actualize the image to display
        ImageWindow * iw = const_cast<ImageWindow *>(currentWnd);
        (*iw).applyParam(param);
    }
      **/




}

bool ParamRepOp::needCurrentImg() const{
    return true;
}

bool ParamRepOp::isValidImgWnd(const genericinterface::ImageWindow* imgWnd) const{
    return imgWnd != NULL;
}
